;
!function() {
	var $ = layui.jquery,
		layer = layui.layer,
		form = layui.form,
		carousel = layui.carousel;

	refCode();
	
	// 背景图片轮播
	carousel.render({
		elem : '#login_carousel',
		width : '100%',
		height : '100%',
		interval : 3000,
		arrow : 'none',
		anim : 'fade',
		indicator : 'none'
	});
	
	// 验证码值存储变量
	var vailCode;

	// 点击刷新验证码
	$("#refCode_login_img").on("click", function() {
		refCode();
	});

	// 获取验证码
	function refCode() {
		$.ajax({
			url : "/getImgCode",
			success : function(result) {
				vailCode = result.rand;
				$("#refCode_login_img").prop("src", "data:image/jpg;base64," + result.image);
				$("#code").val("");
			}
		});
	}

	// 自定义验证规则
	form.verify({
		account : function(value) {
			var regPhone = /^1[34578]\d{9}$/;
			var regEmail = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
			if (!regPhone.test(value.trim()) && !regEmail.test(value.trim())) {
				return "请输入正确的手机号或邮箱";
			}
		},
		code : function(value) {
			if (value.toUpperCase() != vailCode) {
				refCode();
				return "验证码不正确";
			}
		}
	});

	//监听提交  
	form.on("submit(login)", function() {
		$.ajax({
			url : "/secuser/login",
			type : "post",
			data : {
				"account" : $("#account").val(),
				"password" : $("#password").val()
			},
			success : function(result) {
				if (result.status == 0) {
					location = "/";
				} else {
					refCode();
					$("#password").val("");
					layer.msg(result.msg);
				}
			}
		});

		return false;
	});
}();